package sisi.invoices;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import sisi.General;

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class SuppinvoiceWindow extends Window implements 
				org.zkoss.zk.ui.ext.AfterCompose {
//window
private Window windowSuppinvoice, windowEditSuppinvoice;
private Textbox tbCerca;
private Listbox boxSuppinvoice;
private List lista_Suppinvoice;
private boolean lNuovo;
private Toolbarbutton buttonSeleziona;
private ListModelList listModelList;
private Integer idSelezionato = 0;

protected Map<String, Object> args;
private Object SuppinvoiceItem;
private Suppinvoice modSuppinvoice;
// edit
private Textbox type;
private Intbox suppid;
private Textbox curr;
private Textbox stat;
private Checkbox art15;
private Datebox deliverydt;
private Textbox porttoref;
private Textbox paymode;
private Checkbox vatyesno;
private Intbox recnum;
private Textbox bankdoc;
private Datebox bankdtdoc;
private Textbox cctype;
private Doublebox change;
private Textbox clipobject;
private Textbox creditterms;
private Intbox custid;
private Textbox custname;
private Checkbox deleted;
private Textbox dn;
private Datebox dtup;
private Datebox duedate;
private Doublebox exchangerate;
private Textbox initial;
private Datebox invoicedt;
private Datebox invoicedtreg;
private Textbox memento;
private Textbox nfysnote;
private Intbox order;
private Checkbox paid;
private Doublebox paidamount;
private Datebox payddt;
private Intbox portidpass;
private Textbox recbook;
private Textbox suppname;
private Textbox suppnuminvoice;
private Doublebox suppval;
private Textbox tdoc;
private Doublebox totalinvoice;
private Doublebox tottaxed;
private Doublebox totvat;

		public void onCreate$windowSuppinvoice(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		String tastoSeleziona = (String) args.get("tastoSeleziona");
		tastoSeleziona = (tastoSeleziona == null || tastoSeleziona.isEmpty() ? "N": tastoSeleziona);
		boolean lTastoSeleziona = tastoSeleziona.equals("S");
		if (!lTastoSeleziona) {
			buttonSeleziona.setVisible(false);
		}
		idSelezionato = (Integer) args.get("idselezionato");

		Integer numorder = 0, idSupplier = 0;
		
		
		listModelList = new ListModelList();
		lista_Suppinvoice = new SuppinvoiceController().getList(numorder, idSupplier);
		listModelList.addAll(lista_Suppinvoice);
		boxSuppinvoice.setModel(listModelList);
		boxSuppinvoice.setItemRenderer(new SuppinvoiceItemRenderer());

		if (idSelezionato!=null && idSelezionato!=0){
		for (int i = 0; i < lista_Suppinvoice.size(); i++) {
			Suppinvoice record = (Suppinvoice)lista_Suppinvoice.get(i);
			if (record.getRecnum()==idSelezionato.intValue()){
				boxSuppinvoice.setSelectedIndex(i);
				break;
			}
		}
	}

}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	public void onOK$tbCerca(Event event){
		cerca();
	}

	public void onClick$buttonCerca(Event event) {
		cerca();
	}


	private void cerca(){
		listModelList = new ListModelList();
		lista_Suppinvoice = new SuppinvoiceController().getList(tbCerca.getValue());
		listModelList.addAll(lista_Suppinvoice);
		boxSuppinvoice.setModel(listModelList);
		boxSuppinvoice.setItemRenderer(new SuppinvoiceItemRenderer());
	}

	public void onDoubleClicked(Event event) {
		if (buttonSeleziona.isVisible()) {
			seleziona();
		} else {
			modifica(false);
		}
	}

	public void onClick$buttonSeleziona(Event event) {

		seleziona();

}

	public void seleziona() {
		if (boxSuppinvoice.getSelectedIndex() >= 0 && buttonSeleziona.isVisible()) {
			Object oSuppinvoice = boxSuppinvoice.getSelectedItem().getAttribute("rigaSuppinvoice");
			String codiceSelezionato = ""+((Suppinvoice) oSuppinvoice).getRecnum();
			Sessions.getCurrent().setAttribute("codiceSuppinvoiceSelezionato", codiceSelezionato);
			windowSuppinvoice.detach();
		}
	}

	public void onClick$buttonNuovo(Event event) {
		modifica(true);
	}

	public void onClick$buttonModifica(Event event) {
		modifica(false);
	}

	private void modifica(boolean nuovo){
		try {

			int nIndex = boxSuppinvoice.getSelectedIndex();

			if (!nuovo && nIndex == -1) { // nessuna riga selezionata

				Messagebox.show("Select a row to modify", "Information", Messagebox.OK, Messagebox.INFORMATION);

			} else {
				HashMap map = new HashMap();
				if (nuovo){
					map.put("lNuovo", true);
				}
				map.put("boxSuppinvoice", boxSuppinvoice);
				Window finestraEditSuppinvoice = (Window) Executions.createComponents("/editSuppinvoice.zul", null, map);
				finestraEditSuppinvoice.doModal();
			}

		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		}
	}

	public void onClick$buttonCancella(Event event) {
	// TODO Auto-generated method stub

	}

	public class SuppinvoiceItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {
			new Listcell(""+((Suppinvoice) data).getRecnum()).setParent(li);
			new Listcell(""+((Suppinvoice) data).getOrder_()).setParent(li);
			li.setAttribute("rigaSuppinvoice", data);
			ComponentsCtrl.applyForward(li, "onDoubleClick=onDoubleClicked");
		}
	}


// EDIT: NEW/MODIFY
	public void onCreate$windowEditSuppinvoice(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();

		boxSuppinvoice = (Listbox) args.get("boxSuppinvoice");

		if (args.containsKey("lNuovo")) {
			lNuovo = true;
		} else {
			lNuovo = false;
		}

		if (!lNuovo) {
			SuppinvoiceItem = boxSuppinvoice.getSelectedItem().getAttribute("rigaSuppinvoice");
			modSuppinvoice = new SuppinvoiceController().refresh((Suppinvoice) SuppinvoiceItem);
			SuppinvoiceItem = modSuppinvoice;
			windowEditSuppinvoice.setTitle("Edit Suppinvoice");
			//categoryname.setFocus(true);
			//id.setValue(modCategorie.getCategoryid());
			//categoryname.setValue(modCategorie.getCategoryname());
			//description.setValue(modCategorie.getDescription());
			type.setValue(modSuppinvoice.getType());
			suppid.setValue(modSuppinvoice.getSuppid());
			curr.setValue(modSuppinvoice.getCurr());
			stat.setValue(modSuppinvoice.getStat());
			art15.setChecked(modSuppinvoice.getArt15());
			deliverydt.setValue( (java.util.Date) (modSuppinvoice.getDeliverydt()));
			porttoref.setValue(modSuppinvoice.getPorttoref());
			paymode.setValue(modSuppinvoice.getPaymode());
			vatyesno.setChecked(modSuppinvoice.getVatyesno());
			recnum.setValue(modSuppinvoice.getRecnum());
			bankdoc.setValue(modSuppinvoice.getBankdoc());
			bankdtdoc.setValue( (java.util.Date) (modSuppinvoice.getBankdtdoc()));
			cctype.setValue(modSuppinvoice.getCcType());
			change.setValue(modSuppinvoice.getChange());
			clipobject.setValue(modSuppinvoice.getClipobject());
			creditterms.setValue(modSuppinvoice.getCreditterms());
			custid.setValue(modSuppinvoice.getCustid());
			custname.setValue(modSuppinvoice.getCustname());
			deleted.setChecked(modSuppinvoice.getDeleted());
			dn.setValue(modSuppinvoice.getDn());
			dtup.setValue( (java.util.Date) (modSuppinvoice.getDtUp()));
			duedate.setValue( (java.util.Date) (modSuppinvoice.getDuedate()));
			exchangerate.setValue(modSuppinvoice.getExchangerate());
			initial.setValue(modSuppinvoice.getInitial());
			invoicedt.setValue( (java.util.Date) (modSuppinvoice.getInvoicedt()));
			invoicedtreg.setValue( (java.util.Date) (modSuppinvoice.getInvoicedtreg()));
			memento.setValue(modSuppinvoice.getMemento());
			nfysnote.setValue(modSuppinvoice.getNfysNote());
			order.setValue(modSuppinvoice.getOrder());
			paid.setChecked(modSuppinvoice.getPaid());
			paidamount.setValue(modSuppinvoice.getPaidamount());
			payddt.setValue( (java.util.Date) (modSuppinvoice.getPayddt()));
			portidpass.setValue(modSuppinvoice.getPortidpass());
			recbook.setValue(modSuppinvoice.getRecbook());
			suppname.setValue(modSuppinvoice.getSuppname());
			suppnuminvoice.setValue(modSuppinvoice.getSuppnuminvoice());
			suppval.setValue(modSuppinvoice.getSuppval());
			tdoc.setValue(modSuppinvoice.getTdoc());
			totalinvoice.setValue(modSuppinvoice.getTotalinvoice());
			tottaxed.setValue(modSuppinvoice.getTottaxed());
			totvat.setValue(modSuppinvoice.getTotvat());
		} else {
			windowEditSuppinvoice.setTitle("New Suppinvoice");
			//categoryname.setFocus(true);
		}

	}

	public void onClick$btnAnnulla(Event event){
		windowEditSuppinvoice.detach();
	}

	public void onClick$btnConferma(Event event){
		if (lNuovo){
			modSuppinvoice = new Suppinvoice();
		} else {

		}
		String testoMsg = "";

		modSuppinvoice.setType(type.getValue());
		modSuppinvoice.setSuppid(suppid.getValue());
		modSuppinvoice.setCurr(curr.getValue());
		modSuppinvoice.setStat(stat.getValue());
		modSuppinvoice.setArt15(art15.isChecked());
		modSuppinvoice.setDeliverydt( (deliverydt.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(deliverydt.getValue()): null) );
		modSuppinvoice.setPorttoref(porttoref.getValue());
		modSuppinvoice.setPaymode(paymode.getValue());
		modSuppinvoice.setVatyesno(vatyesno.isChecked());
		modSuppinvoice.setRecnum(recnum.getValue());
		modSuppinvoice.setBankdoc(bankdoc.getValue());
		modSuppinvoice.setBankdtdoc( (bankdtdoc.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(bankdtdoc.getValue()): null) );
		modSuppinvoice.setCcType(cctype.getValue());
		modSuppinvoice.setChange(change.getValue());
		modSuppinvoice.setClipobject(clipobject.getValue());
		modSuppinvoice.setCreditterms(creditterms.getValue());
		modSuppinvoice.setCustid(custid.getValue());
		modSuppinvoice.setCustname(custname.getValue());
		modSuppinvoice.setDeleted(deleted.isChecked());
		modSuppinvoice.setDn(dn.getValue());
		modSuppinvoice.setDtUp( (dtup.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(dtup.getValue()): null) );
		modSuppinvoice.setDuedate( (duedate.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(duedate.getValue()): null) );
		modSuppinvoice.setExchangerate(exchangerate.getValue());
		modSuppinvoice.setInitial(initial.getValue());
		modSuppinvoice.setInvoicedt( (invoicedt.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(invoicedt.getValue()): null) );
		modSuppinvoice.setInvoicedtreg( (invoicedtreg.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(invoicedtreg.getValue()): null) );
		modSuppinvoice.setMemento(memento.getValue());
		modSuppinvoice.setNfysNote(nfysnote.getValue());
		modSuppinvoice.setOrder(order.getValue());
		modSuppinvoice.setPaid(paid.isChecked());
		modSuppinvoice.setPaidamount(paidamount.getValue());
		modSuppinvoice.setPayddt( (payddt.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(payddt.getValue()): null) );
		modSuppinvoice.setPortidpass(portidpass.getValue());
		modSuppinvoice.setRecbook(recbook.getValue());
		modSuppinvoice.setSuppname(suppname.getValue());
		modSuppinvoice.setSuppnuminvoice(suppnuminvoice.getValue());
		modSuppinvoice.setSuppval(suppval.getValue());
		modSuppinvoice.setTdoc(tdoc.getValue());
		modSuppinvoice.setTotalinvoice(totalinvoice.getValue());
		modSuppinvoice.setTottaxed(tottaxed.getValue());
		modSuppinvoice.setTotvat(totvat.getValue());

		if (lNuovo){
			new SuppinvoiceController().add(modSuppinvoice);
			testoMsg = "New Suppinvoice: "+ General.trimNull( modSuppinvoice.getOrder_() );
		} else {
			new SuppinvoiceController().update(modSuppinvoice);
			testoMsg = "Updating Suppinvoice: "+ General.trimNull( modSuppinvoice.getOrder_() );
		}

		General.showNotify( testoMsg, windowEditSuppinvoice);

		windowEditSuppinvoice.detach();

		// aggiorna listbox

		if (lNuovo){
			ListModelList lml = (ListModelList) boxSuppinvoice.getListModel();
			lml.add(modSuppinvoice);
			boxSuppinvoice.setSelectedIndex(lml.indexOf(modSuppinvoice));
		} else {
			ListModelList lml = (ListModelList) boxSuppinvoice.getListModel();
			Object oSuppinvoiceItem = boxSuppinvoice.getSelectedItem().getAttribute("rigaSuppinvoice");
			if (lml.indexOf(oSuppinvoiceItem) == -1) {
				// non trovato... quindi non lo aggiorno
			} else {
				lml.set(lml.indexOf(oSuppinvoiceItem), modSuppinvoice);
			}
		}
	}

}
//
// fine
